package com.team.appinfo.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.team.appinfo.entity.AppInfo;
import com.team.appinfo.entity.DevUser;
import com.team.appinfo.service.AppInfoService;
import com.team.appinfo.util.AppinfoDicontion;
import com.team.appinfo.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;

@Controller
public class AppInfoController {
    @Autowired
    private AppInfoService appInfoService;

    /**
     * 分页查询
     * @param appinfoDicontion
     * @param model
     * @return
     */
    @RequestMapping("/searchAppInfo")
    public String searchAppInfo(AppinfoDicontion appinfoDicontion, Model model, HttpSession session){
        DevUser devUser =(DevUser) session.getAttribute("login");
        appinfoDicontion.setDevid(devUser.getId());
        PageInfo<AppInfo> pageInfo = appInfoService.searchAppInfo(appinfoDicontion);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("condition",appinfoDicontion);
        return "developer/appinfolist";
    }

    //新增
    @RequestMapping("/addAppInfo")
    public String addAppInfo(@RequestParam(value ="a_logoPicPath",required = false) CommonsMultipartFile file, AppInfo appInfo,HttpSession session,HttpServletRequest request){
        try {
            String path = "/upload/logo/";
            String realPath = request.getServletContext().getRealPath(path);//绝对路径
            String fileName = FileUploadUtil.uploadFile(realPath, file);
            DevUser devUser =(DevUser) session.getAttribute("login");
            appInfo.setLogolocpath(realPath+"/"+fileName);
            appInfo.setLogopicpath(path+fileName);
            appInfo.setDevid(devUser.getId());
            appInfoService.addAppInfo(appInfo);
            return "redirect:searchAppInfo";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "developer/appinfoadd";
    }

    //修改时回显
    @RequestMapping("/getAppInfo")
    public String getAppInfo(Long id, Model model){
        AppInfo appInfo = appInfoService.getAppInfoById(id);
        model.addAttribute("appInfo",appInfo);
        return "developer/appinfomodify";

    }

    //更新
    @RequestMapping("/updateAppInfo")
    public String updateAppInfo(String oldPicPath,
                                HttpServletRequest request,AppInfo appInfo,
                                @RequestParam(value = "a_logoPicPath",required = false) CommonsMultipartFile file){
        try {
            if (!file.isEmpty()){
                String path = "upload/logo/";
                String realPath = request.getServletContext().getRealPath(path);//绝对路径

                String fileName = FileUploadUtil.uploadFile(realPath, file);
                //从隐藏域中给相当对么转成绝对路径进行删除
                String realPath1 = request.getServletContext().getRealPath(oldPicPath);

                File odFile = new File(realPath1);
                //判断是否有,有就删除没有就算了
                if (odFile.exists()){
                    odFile.delete();
                }
                //将新上传的图片路径保存到数据
                appInfo.setLogopicpath(path+fileName);//相对路径
                appInfo.setLogolocpath(realPath+"/"+fileName);//绝对路径
            }
            appInfoService.updateAppInfo(appInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
            return "redirect:getAppInfo?id="+appInfo.getId();
    }

    //删除
    @RequestMapping(value = "/deleteAppInfo")
    public String deleteAppInfo(HttpServletRequest request,Long id,String olpath){
        try {
            appInfoService.delAppInfo(id);
            String realPath = request.getServletContext().getRealPath(olpath);
            File file = new File(realPath);
            if (file.exists())
                file.delete();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:searchAppInfo";
    }
}
